package com.cooba.video_frament;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.activitys.VideoDetailsActivity;
import com.cooba.bean.TuiJianBean;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.cooba.utils.XGridView;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

/**
 * Created by Administrator on 2015/12/29.
 * 推荐
 */
public class RecommendFrament extends Fragment implements View.OnClickListener {
    private XGridView recom_gv1, recom_gv2, recom_gv3;
    private View view;
    private HttpUtils httpUtils;
    private RequestParams params;
    private TextView tv2, tv4, tv6;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        try {
            if (view == null) {
                view = initview(inflater);
            }
            return view;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private View initview(LayoutInflater inflater) {
        View inflate = inflater.inflate(R.layout.recommend_fragment, null, false);
        init(inflate);
        return inflate;
    }

    private void init(View inflate) {
        recom_gv1 = (XGridView) inflate.findViewById(R.id.recom_gv1);
        recom_gv2 = (XGridView) inflate.findViewById(R.id.recom_gv2);
        recom_gv3 = (XGridView) inflate.findViewById(R.id.recom_gv3);
        tv2 = (TextView) inflate.findViewById(R.id.tv2);
        tv4 = (TextView) inflate.findViewById(R.id.tv4);
        tv6 = (TextView) inflate.findViewById(R.id.tv6);
        tv2.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv6.setOnClickListener(this);
        httpUtils = new HttpUtils();
        params = new RequestParams();
        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.queryVideoRecoList, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.e("sss", "获取失败");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                paserData(responseInfo.result);
            }
        });

        recom_gv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String videoUrl = mAdapter1.getList().get(position).getVideo_page_url().toString().replaceAll("\\\\", "");
                String videoTitle = mAdapter1.getList().get(position).getTitle().toString();
                String videoDs = mAdapter1.getList().get(position).getIntro().toString();
                Intent intent = new Intent(getActivity(), VideoDetailsActivity.class);
                intent.putExtra("videoUrl", videoUrl);
                intent.putExtra("videoTitle", videoTitle);
                intent.putExtra("videoDs", videoDs);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zcdh_set_in, R.anim.zcdh_alpha_out);
            }
        });
        recom_gv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String videoUrl = mAdapter2.getList().get(position).getVideo_page_url().toString().replaceAll("\\\\", "");
                String videoTitle = mAdapter2.getList().get(position).getTitle().toString();
                String videoDs = mAdapter2.getList().get(position).getIntro().toString();
                Intent intent = new Intent(getActivity(), VideoDetailsActivity.class);
                intent.putExtra("videoUrl", videoUrl);
                intent.putExtra("videoTitle", videoTitle);
                intent.putExtra("videoDs", videoDs);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zcdh_set_in, R.anim.zcdh_alpha_out);
            }
        });
        recom_gv3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String videoUrl = mAdapter3.getList().get(position).getVideo_page_url().toString().replaceAll("\\\\", "");
                String videoTitle = mAdapter3.getList().get(position).getTitle().toString();
                String videoDs = mAdapter3.getList().get(position).getIntro().toString();
                Intent intent = new Intent(getActivity(), VideoDetailsActivity.class);
                intent.putExtra("videoUrl", videoUrl);
                intent.putExtra("videoTitle", videoTitle);
                intent.putExtra("videoDs", videoDs);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zcdh_set_in, R.anim.zcdh_alpha_out);
            }
        });
    }

    private VideoMyadapter1 mAdapter1;
    private VideoMyadapter2 mAdapter2;
    private VideoMyadapter3 mAdapter3;

//    private List<TuiJianBean.HotListEntity> list1 = new ArrayList<>();
//    private List<TuiJianBean.RecreatListEntity> list2 = new ArrayList<>();
//    private List<TuiJianBean.RecreatListEntity> list3 = new ArrayList<>();

    private void paserData(String result) {
        SharedPreferencesUtil.saveData(getActivity(), "TuiJian", result);
        TuiJianBean shitingBean = new Gson().fromJson(result, TuiJianBean.class);


        mAdapter1 = new VideoMyadapter1(getContext(), shitingBean.hotList);
        recom_gv1.setAdapter(mAdapter1);
//        list1.addAll(shitingBean.hotList);
//        mAdapter1.setList(list1);
        mAdapter2 = new VideoMyadapter2(getContext(), shitingBean.recreatList);
        recom_gv2.setAdapter(mAdapter2);

        mAdapter3 = new VideoMyadapter3(getContext(), shitingBean.varietyList);
        recom_gv3.setAdapter(mAdapter3);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv2:
//                Intent intent = new Intent(getActivity(), VideoHotFrament.class);
//                startActivity(intent);
                break;

            case R.id.tv4:
                break;

            case R.id.tv6:
                break;
        }
    }

    class VideoMyadapter1 extends BaseAdapter {
        private Context context;
        private List<TuiJianBean.HotListEntity> list;

        public List<TuiJianBean.HotListEntity> getList() {
            return list;
        }

        public void setList(List<TuiJianBean.HotListEntity> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public VideoMyadapter1(Context context, List<TuiJianBean.HotListEntity> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            VideoViewHolder holder = null;
            if (convertView == null) {
                holder = new VideoViewHolder();
                convertView = LayoutInflater.from((context)).inflate(R.layout.video_list_item, parent, false);
                holder.img = (ImageView) convertView.findViewById(R.id.iv_video);
                holder.description = (TextView) convertView.findViewById(R.id.tv_video);
                holder.title = (TextView) convertView.findViewById(R.id.tv_videomsg);
                convertView.setTag(holder);
            } else {
                holder = (VideoViewHolder) convertView.getTag();
            }

            holder.title.setText(list.get(position).getTitle());
            holder.description.setText(list.get(position).getIntro());
            String imgUrl = list.get(position).getImg_url();

            BitmapUtils bitmapUtils = new BitmapUtils(context);
            bitmapUtils.display(holder.img, imgUrl);

            return convertView;
        }

        class VideoViewHolder {

            private ImageView img;
            public TextView title;
            public TextView description;

        }

    }

    class VideoMyadapter2 extends BaseAdapter {
        private Context context;
        private List<TuiJianBean.RecreatListEntity> list;

        public List<TuiJianBean.RecreatListEntity> getList() {
            return list;
        }

        public void setList(List<TuiJianBean.RecreatListEntity> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public VideoMyadapter2(Context context, List<TuiJianBean.RecreatListEntity> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            VideoViewHolder holder = null;
            if (convertView == null) {
                holder = new VideoViewHolder();
                convertView = LayoutInflater.from((context)).inflate(R.layout.video_list_item, parent, false);
                holder.img = (ImageView) convertView.findViewById(R.id.iv_video);
                holder.description = (TextView) convertView.findViewById(R.id.tv_video);
                holder.title = (TextView) convertView.findViewById(R.id.tv_videomsg);
                convertView.setTag(holder);
            } else {
                holder = (VideoViewHolder) convertView.getTag();
            }

            holder.title.setText(list.get(position).getTitle());
            holder.description.setText(list.get(position).getIntro());
            String imgUrl = list.get(position).getImg_url();

            BitmapUtils bitmapUtils = new BitmapUtils(context);
            bitmapUtils.display(holder.img, imgUrl);

            return convertView;
        }

        class VideoViewHolder {

            private ImageView img;
            public TextView title;
            public TextView description;

        }

    }

    class VideoMyadapter3 extends BaseAdapter {
        private Context context;
        private List<TuiJianBean.VarietyListEntity> list;

        public List<TuiJianBean.VarietyListEntity> getList() {
            return list;
        }

        public void setList(List<TuiJianBean.VarietyListEntity> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public VideoMyadapter3(Context context, List<TuiJianBean.VarietyListEntity> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            VideoViewHolder holder = null;
            if (convertView == null) {
                holder = new VideoViewHolder();
                convertView = LayoutInflater.from((context)).inflate(R.layout.video_list_item, parent, false);
                holder.img = (ImageView) convertView.findViewById(R.id.iv_video);
                holder.description = (TextView) convertView.findViewById(R.id.tv_video);
                holder.title = (TextView) convertView.findViewById(R.id.tv_videomsg);
                convertView.setTag(holder);
            } else {
                holder = (VideoViewHolder) convertView.getTag();
            }

            holder.title.setText(list.get(position).getTitle());
            holder.description.setText(list.get(position).getIntro());
            String imgUrl = list.get(position).getImg_url();

            BitmapUtils bitmapUtils = new BitmapUtils(context);
            bitmapUtils.display(holder.img, imgUrl);

            return convertView;
        }

        class VideoViewHolder {

            private ImageView img;
            public TextView title;
            public TextView description;

        }

    }
    class VideoMyadapter4 extends BaseAdapter {
        private Context context;
        private List<TuiJianBean.MovieListEntity> list;

        public VideoMyadapter4(Context context, List<TuiJianBean.MovieListEntity> list) {
            this.context = context;
            this.list = list;
        }

        public List<TuiJianBean.MovieListEntity> getList() {
            return list;
        }

        public void setList(List<TuiJianBean.MovieListEntity> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            VideoViewHolder holder = null;
            if (convertView == null) {
                holder = new VideoViewHolder();
                convertView = LayoutInflater.from((context)).inflate(R.layout.video_list_item, parent, false);
                holder.img = (ImageView) convertView.findViewById(R.id.iv_video);
                holder.description = (TextView) convertView.findViewById(R.id.tv_video);
                holder.title = (TextView) convertView.findViewById(R.id.tv_videomsg);
                convertView.setTag(holder);
            } else {
                holder = (VideoViewHolder) convertView.getTag();
            }

            holder.title.setText(list.get(position).getTitle());
            holder.description.setText(list.get(position).getIntro());
            String imgUrl = list.get(position).getImg_url();

            BitmapUtils bitmapUtils = new BitmapUtils(context);
            bitmapUtils.display(holder.img, imgUrl);

            return convertView;
        }

        class VideoViewHolder {

            private ImageView img;
            public TextView title;
            public TextView description;

        }

    }
}